﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Linq.Expressions;

namespace CommonUtils
{
    public class DbContextPlus<TDb> : DbContext where TDb : DbContext
    {
        public DbContextPlus(DbContextOptions<TDb> options) : base(options)
        {
            "我是数据库构造行数1,我被调用了".Print();
        }

        public DbContextPlus(DbContextOptions<TDb> options, ILogger<DbContextPlus<TDb>> logger) : base(options)
        {
            logger.LogInformation("我是数据库构造行数2,我被调用了");
        }

        public IQueryable<TEntity> FromSql<TEntity>(string sql) where TEntity : DbEntity
        => Set<TEntity>().FromSqlRaw(sql);

        //protected override void OnModelCreating(ModelBuilder builder)
        //{
        //    //唯一
        //    builder.Entity<User>(eb =>
        //    {
        //        eb.HasIndex(e => e.Account).IsUnique();
        //    });
        //    //索引
        //    builder.Entity<User>(eb =>
        //    {
        //        eb.HasIndex(e => e.Account);
        //    });
        //}

        //static DbContextPlus()
        //{
        //    //联表
        //    DbContextUtil.SetInclude<User>(m => m.Roles);
        //}

        public static Expression<Func<TEntity, bool>> NewQuery<TEntity>() where TEntity : DbEntity
        => LambdaUtil.True<TEntity>();

        //public static WebApplicationBuilder InstanceProvider;
        public static IServiceCollection InstanceProvider;

        public static TDb Instance
        {
            get
            {
                //return InstanceProvider.GetService<TDb>();
                if (InstanceProvider == null)
                    InstanceProvider = CoreUtil.Services;
                return InstanceProvider.Get<TDb>();
            }
        }

        public static IOrderedQueryable<TEntity> NewOrder<TEntity, TKey>(Expression<Func<TEntity, TKey>> keySelector) where TEntity : DbEntity
        => Instance.NewOrder(keySelector);

        public static IOrderedQueryable<TEntity> NewOrderDesc<TEntity, TKey>(Expression<Func<TEntity, TKey>> keySelector) where TEntity : DbEntity
        => Instance.NewOrderDesc(keySelector);

        public static bool Exist<TEntity>(TEntity item) where TEntity : DbEntity
        => Instance.Exist(item);

        public static TEntity Query<TEntity>(string id) where TEntity : DbEntity
        => Instance.Query<TEntity>(id);

        public static TEntity Query<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : DbEntity
        => Instance.Query(predicate);

        public static TEntity[] List<TEntity>() where TEntity : DbEntity
        => Instance.List<TEntity>();

        public static TEntity[] List<TEntity>(Expression<Func<TEntity, bool>> predicate, IOrderedQueryable<TEntity> order = null) where TEntity : DbEntity
        => Instance.List(predicate, order);

        public static Page<TEntity> Page<TEntity>(int? pageNum, int? pageSize, Expression<Func<TEntity, bool>> predicate, IOrderedQueryable<TEntity> order = null)
            where TEntity : DbEntity
        => Instance.Page(pageNum, pageSize, predicate, order);

        public static TEntity Save<TEntity>(TEntity item) where TEntity : DbEntity
        => Instance.Save(item);

        public static TEntity[] Save<TEntity>(TEntity[] items) where TEntity : DbEntity
        => Instance.Save(items);

        public static bool Delete<TEntity>(TEntity item) where TEntity : DbEntity
        => Instance.Delete(item);

        public static int Delete<TEntity>(TEntity[] items) where TEntity : DbEntity
        => Instance.Delete(items);
    }
}
